﻿Option Explicit On
Imports Global_Visas_Business_Layer
Imports System.Data
Public Class frmAddressBook

    Public SelectedAddress() As String
    Public SelectedAddresses As String

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Erase Me.SelectedAddress
        Me.Hide()
    End Sub

    Private Sub AddressBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.LoadContacts(CommonDataFunctions.AddressTypes.GV)

    End Sub

    Private Sub ViewChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radClient.Checked, radGV.Checked, radSystem.Checked

        If Me.radGV.IsChecked Then
            Me.LoadContacts(CommonDataFunctions.AddressTypes.GV)
        ElseIf Me.radClient.IsChecked Then
            Me.LoadContacts(CommonDataFunctions.AddressTypes.Client)
        ElseIf Me.radSystem.IsChecked Then
            Me.LoadContacts(CommonDataFunctions.AddressTypes.System)
        End If


    End Sub

    Private Sub LoadContacts(ByVal AddressType As People.AddressTypes)

        'With Me.lstContacts
        '    .DisplayMember = "personname"
        '    .ValueMember = "EmailAddress"
        '    .DataSource = CommonDataFunctions.GetAddressBook(AddressType)
        '    .SelectedIndex = -1
        'End With
        Dim dt As DataTable
        Try


            dt = CommonDataFunctions.GetAddressBook(AddressType)
            'lstContacts.ItemsSource = dt
            DataBinding.BindListView(dt, lstContacts)
        Catch ex As Exception
        Finally
            dt = Nothing
        End Try
    End Sub

    Private Sub lstContacts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.lstContacts.SelectedIndex = -1 Then
            Me.btnAdd.IsEnabled = False
        Else
            Me.btnAdd.IsEnabled = True
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim lvwItem As Object
        Dim strSelectedAddresses As String = String.Empty

        If Me.lstContacts.SelectedIndex >= 0 Then
            For Each lvwItem In lstContacts.SelectedItems
                strSelectedAddresses = lvwItem.item(1).ToString & ";" & strSelectedAddresses
            Next
            Me.SelectedAddresses = strSelectedAddresses
            'Me.SelectedAddress = CType(Me.lstContacts.SelectedItem, DataRowView).Item(1).ToString
        End If

        Me.Hide()

    End Sub
End Class